# 用async io和request 实现以下功能：异步get某些网站, 并在网站开始和结束访问时给出print
# 用get 访问以下就行
# 参考库 aiohttp， asyncio

import asyncio
import aiohttp


# 定义一个异步函数fetch_page来获取每个网站内容
async def fetch_page(url):
    print(f"开始异步get: {url}")
    # 使用aiohttp的ClientSession发起异步请求
    async with aiohttp.ClientSession() as session:
        async with session.get(url):
            print(f"结束异步get: {url}")


async def main():
    web_list = [
        "https://www.baidu.com",
        "https://bing.com",
        "http://www.google.com"
    ]

    tasks = []
    for url in web_list:
        tasks.append(fetch_page(url))

    # asyncio.gather同时await多个任务
    await asyncio.gather(*tasks)


if __name__ == "__main__":
    asyncio.run(main())
